package com.travel.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.travel.entity.User;
import com.travel.service.UserService;
import com.travel.service.impl.UserServiceImpl;
import com.travel.util.PageInfo;

/**
 * Servlet implementation class ScenicServlet
 */
@WebServlet("/index")
@MultipartConfig
public class indexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserService userService = new UserServiceImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public indexServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String op = request.getParameter("op");
		if (op == null) {
			doFindUser(request, response);
		} else if ("delete".equals(op)) {
			doDeleteUser(request, response);
		} else if ("searchByUser".equals(op)) {
			dosearchByUser(request, response);
		} else if ("searchByPage".equals(op)) {
			dosearchByPage(request, response);
		}
	}

	private void dosearchByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		int pageNum = Integer.parseInt(request.getParameter("pageNum"));

		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		String userName = request.getParameter("keyword");
		if (userName == null) {
			userName = "";
		}
		PageInfo<User> pageInfo = userService.getUserListByPage(pageNum, pageSize, userName);
		// 封装数据
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(pageInfo);
		// 设置响应内容类型
		response.setContentType("application/json;charset=utf-8");
		// 响应数据
		PrintWriter out = response.getWriter();
		out.print(json);
	}

	private void dosearchByUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		String userName = request.getParameter("keyword");
		List<User> userlist = userService.findByNameAndPassword(userName);
		int pageNum = Integer.parseInt(request.getParameter("pageNum"));

		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		if (userName == null) {
			userName = "";
		}
		PageInfo<User> pageInfo = userService.getUserListByPage(pageNum, pageSize, userName);
		// 封装数据
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(pageInfo);
		// 设置响应内容类型
		response.setContentType("application/json;charset=utf-8");
		// 响应数据
		PrintWriter out = response.getWriter();
		out.print(json);
	}

	private void doDeleteUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		String userId = request.getParameter("userId");
		userService.deletById(Integer.parseInt(userId));
		int pageNum = Integer.parseInt(request.getParameter("pageNum"));

		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		String userName = request.getParameter("keyword");
		PageInfo<User> pageInfo = userService.getUserListByPage(pageNum, pageSize, userName);
		// 封装数据
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(pageInfo);
		System.out.println(json);
		// 设置响应内容类型
		response.setContentType("application/json;charset=utf-8");
		// 响应数据
		PrintWriter out = response.getWriter();
		out.print(json);
	}

	private void doFindUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 查询
		List<User> userlist = userService.find();
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(userlist);
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.print(json);
	}

}
